package servlet;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.QuestionaireBeanRemote;
import beans.TestBean;
import dto.QuizzeDTO;

/**
 * Servlet implementation class TestServlet
 */
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @EJB
    private QuestionaireBeanRemote sf2;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
	super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	processRequest(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	processRequest(request, response);
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	try {
	    final List<QuizzeDTO> allQuizzes = sf2.getAllQuizzes();
	    TestBean testBean = new TestBean();

	    String s = "<body bgcolor=\"#E6E6FA\"><br />";
	    for (int i = 0; i < allQuizzes.size(); i++) {
		s += "<center>" + allQuizzes.get(i).getName() + "</center>";
		for (int j = 0; j < allQuizzes.get(i).getQuestions().size(); j++)
		    s += "<center>" + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + allQuizzes.get(i).getQuestions().get(j).getText() + "<center>";
		s += "<br />";
	    }
	    testBean.setAllQuestions(s);

	    request.getSession().setAttribute("quizzes", testBean);
	    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/search.jsp");
	    dispatcher.forward(request, response);
	} finally {
	}
    }

}
